package com.glory.gloryquickui.viewmodel

import android.content.Context
import androidx.fragment.app.Fragment
import com.glory.gloryquickui.base.BaseViewModel
import com.glory.gloryquickui.entity.HomeBean
import com.glory.gloryquickui.fragment.QuickUIFragment
import com.glory.gloryquickui.utils.Utils
import com.glory.gloryquickui.widget.tablayout.listener.CustomTabEntity
import com.glory.hiwork.mvvm.bean.CallObserveBean
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.io.InputStream
import java.util.*

/**
 * FileName: HomeViewModel
 * Author: XJH
 * Date: 2021/5/27 15:21
 * Description:
 * History:
 */
class HomeViewModel : BaseViewModel() {

    var mEntity = HomeBean()
    val mFragments = arrayListOf<Fragment>()
    val mTabEntities = arrayListOf<CustomTabEntity>()

    fun getHomeInfo(context : Context){
        //模拟网络请求
        val mJsonStream: InputStream = context.resources.assets.open("home.txt")
        val mJson = Utils.getString(mJsonStream)
        mEntity = Gson().fromJson(mJson, object : TypeToken<HomeBean>() {}.type)
        if (mEntity.tabList.isEmpty()){
            defUI.callObserve.value = CallObserveBean(404,0)
        }else{
            getHomeData(context)
        }
    }


    private fun getHomeData(context: Context){
        mEntity.tabList.forEach{
            mFragments.add(QuickUIFragment(it.fromID))
            it.context = context
            mTabEntities.add(it)
        }

        defUI.callObserve.value = CallObserveBean(200,0)
    }
}